package com.imonsoft.sqm.web.business.supplier_quality;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.imonsoft.common.constant.CCommon;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.service.business.supplier_quality.AuditService;
import com.imonsoft.sqm.vo.business.supplier_quality.AuditVo;

@Controller
@RequestMapping("/business/supplier_quality/audit")
public class AuditControl {

	
	protected final Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private  AuditService auditService;
	
	@Autowired
	private SupplierService supplierService;
	
	@RequestMapping("/listUI.do")
	public void listUI() {
	}
	
	
	@RequestMapping(value = "/ajaxList.do", method = RequestMethod.POST)
	public void ajaxList(HttpServletResponse response, Pager pager, AuditVo auditVo) {
		DataGrid<AuditVo> dataGrid = new DataGrid<AuditVo>();
		try {
			dataGrid = auditService.getDataGridVoBy(auditVo, pager);
		} catch (SystemException se) {
			logger.debug(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
	}
	
	

	@RequestMapping(value = "/ajaxAllList.do", method = RequestMethod.POST)
	public void ajaxAllList(HttpServletResponse response, Pager pager, AuditVo auditVo) {
		DataGrid<AuditVo> dataGrid = new DataGrid<AuditVo>();
		try {
			dataGrid = auditService.getDataGridVoBySupplier(auditVo, pager);
		} catch (SystemException se) {
			logger.debug(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
	}
	
	
	@RequestMapping(value = "/addUI.do")
	public void addUI(ModelMap model) {

	}
	
	@RequestMapping(value = "/processaddUI.do")
	public void processaddUI(ModelMap model) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String id=request.getParameter("id");
		Supplier supplier=	supplierService.get(Long.valueOf(id));
		model.put("vo", supplier);
	}
	
	@RequestMapping(value = "/ajaxAdd.do", method = RequestMethod.POST)
	public void ajaxAdd(HttpServletResponse response, AuditVo auditVo,BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		try {
			auditService.txAddAudit(auditVo,message,result);
		} catch (SystemException se) {
			se.printStackTrace();
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
	
	@RequestMapping(value = "/ajaxDelete.do", method = RequestMethod.POST)
	public void ajaxDelete(HttpServletResponse response,@RequestParam("ids") List<Long> ids) {
		ReturnMessage message = new ReturnMessage();
		try {
			auditService.deleteAudit(ids);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
	
	@RequestMapping(value = "/modifyUI.do")
	public void modifyUI(ModelMap model, Long id) {
		if (null != id) {
			AuditVo vo;
			try {
				vo = auditService.getAuditVoByid(id);
				model.put("vo", vo);
			} catch (Exception e) {
				logger.error(this.getClass() + ":" + e.getMessage());
			}
			
		} else {
			new Exception(CCommon.EXCEPTION_MSG);
		}
	}

	
	
	@RequestMapping(value = "/ajaxModify.do", method = RequestMethod.POST)
	public void ajaxModify(HttpServletResponse response ,AuditVo auditVo,BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		try {
			auditService.txUpdateOrAddAudit(auditVo);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			e.printStackTrace();
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
	
	@RequestMapping(value = "/processUI.do")
	public void processUI(ModelMap model, Long id) {
		if(id==null){
			new Exception(CCommon.EXCEPTION_MSG);
		}
		Supplier supplier=	supplierService.get(id);
		model.put("vo", supplier);
	}
	
}
